package com.lucky.back.service.impl;

import com.lucky.back.ex.ServiceException;
import com.lucky.back.mapper.ProductCategoryMapper;
import com.lucky.back.pojo.dto.ProductCategoryAddNewDTO;
import com.lucky.back.pojo.entity.Product;
import com.lucky.back.pojo.entity.Productcategory;
import com.lucky.back.pojo.vo.*;
import com.lucky.back.service.ICategoryService;
import com.lucky.back.web.ServiceCode;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Slf4j
@Service
public class CategoryServiceImpl implements ICategoryService {

    @Autowired
    ProductCategoryMapper categoryMapper;

    @Override
    public void updateById(Productcategory productcategory) {
        log.debug("开始处理修改大分类请求");
        categoryMapper.updateById(productcategory);
    }

    @Override
    public ProductCategoryStandardVO getStandardById(Long id) {

        log.debug("开始处理【根据id查询大分类详情】的业务，参数：{}", id);
        ProductCategoryStandardVO productCategoryStandardVO = categoryMapper.getStandardById(id);
        if (productCategoryStandardVO == null) {
            String message = "获取大分类详情失败，尝试访问的数据不存在！";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERR_NOT_FOUND, message);
        }
        return productCategoryStandardVO;
    }

    @Override
    public void addNew(ProductCategoryAddNewDTO productCategoryAddNewDTO) {
        int count = categoryMapper.countByName(productCategoryAddNewDTO.getCategory());
        if (count != 0){
            throw new ServiceException(ServiceCode.ERR_CONFLICT,"添加失败,名称已被占用");
        }
        Productcategory productcategory = new Productcategory();
        BeanUtils.copyProperties(productCategoryAddNewDTO,productcategory);
        System.out.println("即将执行插入分类service");
        categoryMapper.insert(productcategory);
    }

    @Override
    public void delete(Long id) {
        log.debug("开始处理删除请求");
        ProductCategoryStandardVO queruResult = categoryMapper.getStandardById(id);
        if (queruResult == null){
            String message = "删除分类失败,,尝试访问数据不存在!";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERR_NOT_FOUND,message);
        }
        log.debug("即将删除分类数据");
        categoryMapper.deleteById(id);
        log.debug("删除分类，完成！");
    }

    @Override
    public List<ProductCategoryListItemVO> list() {
        log.debug("查询种类列表服务层——————————————————");
        return categoryMapper.list();
    }

    @Override
    public List<Long> supIdList() {

        return categoryMapper.supIdList();
    }
}
